package com.zbhk.qc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zbhk.qc.dto.ArticleVO;
import com.zbhk.qc.dto.ResultVO;
import com.zbhk.qc.entity.VdPublicArticle;
import com.zbhk.qc.service.ArticleServer;
import com.zbhk.qc.util.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("ArticleController")
@RequestMapping(value = "/article")
@RequiredArgsConstructor
@Api(tags = "文章相关接口")
public class ArticleController {


    @Autowired
    private ArticleServer articleServer;

    @GetMapping("/getArticleList")
    @ApiOperation(value = "查询文章数据", notes = "查询文章分页数据")
    public IPage<VdPublicArticle> getArticleList(
            @ApiParam(value = "公众号名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页数") @RequestParam(required = true ,defaultValue = "10") Integer pageSize,
            @ApiParam(value = "页码") @RequestParam(required = true,defaultValue = "1") Integer pageNumber) {
        IPage<VdPublicArticle> list = null;
        try {
            list = articleServer.getArticleList(name,pageSize,pageNumber);
        }catch (Exception e) {
            return list;
        }
        return list;
    }

    @ApiOperation(value = "新增文章", notes = "", produces = "application/json")
    @PostMapping("save")
    public ResultVO save(@Valid @ModelAttribute ArticleVO articleVO) throws Exception {
        try {
            articleServer.save(articleVO);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success(articleVO);

    }

    @ApiOperation(value = "修改文章", notes = "", produces = "application/json")
    @PostMapping("update")
    public ResultVO update(@Valid @ModelAttribute ArticleVO articleVO) throws Exception {
        VdPublicArticle article = new VdPublicArticle();
        try {
            article = articleServer.update(articleVO);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success(article);
    }

    @ApiOperation(value = "删除文章", notes = "", produces = "application/json")
    @GetMapping("delete")
    public ResultVO delete(@ApiParam(value = "文章编号") @RequestParam(required = true) Long id) throws Exception {
        try {
            articleServer.delete(id);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success("成功");
    }

    @ApiOperation(value = "查看文章", notes = "", produces = "application/json")
    @GetMapping("get")
    public ResultVO get(@ApiParam(value = "文章编号") @RequestParam(required = true) Long id) throws Exception {
        VdPublicArticle article = new VdPublicArticle();
        try {
            article = articleServer.get(id);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success(article);
    }




}
